<?php
namespace Admin\Controller;
use Think\Controller;
class PictureController extends Controller {
    //图片显示
public function picture()
{
    $picture = new pictureModel();
    $showpicture = $photo ->findall();
    $this -> assignsign("showpicture",$showphoto);
    $this -> display();
}
    //显示图片列表信息
	public function picturelist(){

        $pictureinf=M('pictureinf');
        $list = $pictureinf->select();
        //var_dump($list);
        $this->assign('data',$list);// 赋值数据集
     	$this->display();
    }

    //显示图片编辑页面信息
    public function editpicture(){

        $id=$_GET['id'];
        $pictureinf=M('pictureinf');
        $list = $pictureinf->where("picinf_id = $id ")->select();
        //var_dump($list);
        $this->assign('data',$list);// 赋值数据集
        $this->display();   
    }

    //获取图片编辑页面信息并添加到数据库
    public function doeditpicture(){

         //获取表单数据,通过标签中的name属性。
        $picinf_id = I('post.picinfid');
        $picintr = I('post.picintr');
        $clubid = I('post.clubid');
        $usrid = I('post.usrid');
        $piccover = I('post.piccover');
        $submitpeople = I('post.submitpeople');
        $commenttimes = I('post.commenttimes');
        $publishtime = I('post.publishtime');

        //保存数据到data数组
        $data['pic_intr'] = $picintr;
        $data['clubintr_id'] =  $clubid;
        $data['usr_id'] = $usrid;
        $data['pic_cover'] = $piccover;
        $data['comment_times'] = $commenttimes;
        $data['submit_people'] = $submitpeople;
        $data['publish_time'] = $publishtime;
        
        $pictureinf=M('pictureinf');
        $result = $pictureinf->where("picinf_id = $picinf_id")->save($data);
        if($result==FALSE){
            $this->error("修改失败！");
        }else{
            $this->success("修改成功！");
        }
     
    }

    //删除图片信息
    public function deletepic(){

        $picinf_id=$_GET['id'];
        //var_dump($clubintr_id);
        $pictureinf=M('pictureinf');

        if($pictureinf->where("picinf_id=$picinf_id")->delete())
        {
            $this->success('删除成功',U('Picture/picturelist'));
        }
        else{

            $this->error('删除失败');
        }
        $this->display();
    }

    //显示图片评论表信息(有点复杂)
    public function picturecomment(){

         $piccomment=M('piccomment');
         $list = $piccomment->select();

         //构造数据
         foreach($list as $key=>$value)
         { 
              foreach($value as $key2=>$value2){ 

                 if($key2=='response_flag')
                 {
                    if(!$value2)//说明是评论记录,开始重组新数组$listg
                    {   

                        $listg[$key]['ID'] =  $list[$key]['pcomorres_id'];
                        $listg[$key]['comment'] =  $list[$key]['pcomorres_content'];

                        //找出对应的回复记录
                        $pid = $list[$key]['pcomorres_id'];
                        $list2 = $piccomment->where("response_flag = $pid")->select();
                        
                        //找出评论发起者
                        $user=M('users');
                        $uid = $list[$key]['usr_id'];
                        $ul = $user->where("usr_id=$uid")->select();
                        $listg[$key]['user'] = $ul['0']['user_name'];

                        //找出评论的回复者
                        if(!$list2){
                             $listg[$key]['author'] = NULL;
                             $listg[$key]['huifu'] = NULL;
                        }else{
                            
                            $uid2 = $list2['0']['usr_id'];
                            $ul2 = $user->where("usr_id=$uid2")->select();
                            $listg[$key]['author'] = $ul2['0']['user_name'];
                            $listg[$key]['huifu'] = $list2['0']['pcomorres_content']; 
                        }
                        
                        $this->assign('data',$listg);// 赋值数据集
                        //var_dump($listg);
                       
                    }
                   
                 }
              }
         } 

        $this->display();
    }

     //删除图片评论信息
     public function deletecomment(){

        $pcomorres_id=$_GET['id'];
        var_dump($pcomorres_id);
       
        $piccomment=M('piccomment');

        if($piccomment->where("pcomorres_id=$pcomorres_id")->delete())
        {
            $this->success('删除成功',U('Picture/picturecomment'));
        }
        else{

            $this->error('删除失败');
        }

    }

    public function searchpic(){

        $key = $_GET['key'];
        $picnum = M('pictureinf')->where('submit_people like "%'.$key.'%"')->count();
        $Page = new \Think\Page($picnum,2);
        $this->picnum =$picnum;
        $this->picpage=$Page->show();
        $this->piclist=M('pictureinf')
            ->where('submit_people like "%'.$key.'%"')
            ->order('picinf_id')
            ->limit($Page->firstRow,2)
            ->select();

        $this->display();
    }

}